package cn.regexp.web.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopGoods;
import cn.regexp.query.GoodsQuery;
import cn.regexp.service.IGoodsService;
import cn.regexp.service.ISearchHistoryService;
import cn.regexp.utils.JsonResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("searchHistory")
public class SearchHistoryController {
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISearchHistoryService searchHistoryService;

    @GetMapping("/search")
    public JsonResult<IPage<ShopGoods>> search(GoodsQuery qo, HttpServletRequest request) {
        if (ObjectUtil.isEmpty(qo.getKeyword())) {
            return JsonResult.defaultError();
        }
        IPage<ShopGoods> goodsIPage = goodsService.queryGoods(qo);
        String token = request.getHeader("token");
        searchHistoryService.saveSearchHistory(qo.getKeyword(), token);
        return JsonResult.success(goodsIPage);
    }
}
